﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using University.Workshop.Business;
using University.Workshop.Model;
using University.Workshop.DataAccess;

namespace University.Workshop.WebApp
{
    public partial class ViewAuction : System.Web.UI.Page
    {
        private Auction auction;
        private long prodId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
            prodId = long.Parse(Request["id"]);
            String prodName = Request["name"];
            try
            {
                auction = AuctionDataAccess.GetAuctionByproductId(prodId);
            }
            catch (Exception exc)
            {
                Response.Write("<script>alert('" + exc.Message + "'); window.location.href = 'UserProducts.aspx';</script>");
            }

            if (auction != null)
                TbProductName.Text = prodName;
            if (auction != null)
            {
                TbDate.Text = auction.Date.ToString();
                TbLastBidDate.Text = auction.LastBidDate.ToString();
                TbLastPrice.Text = auction.LastBidPrice.ToString();
                lbCurrency.Text = auction.Currency;
            }
            try
            {
                TbLastBidder.Text = UserDataAccess.GetUserById(auction.BidderUserId).ContactInformation.Email;
                long ownerEmail = ProductDataAccess.GetProductById(auction.ProductId).SellerId;
                if (auction.BidderUserId == ownerEmail)
                    btnCancelAuction.Enabled = true;
                else btnCancelAuction.Enabled = true;
            }
            catch(Exception)
            {
                Response.Write("<script>alert('Cannot get last bidder user');</script>");
            }

        }

        protected void btnCancelAuction_Click(object sender, EventArgs e)
        {
            Response.Redirect("CancelAuction.aspx?id=" + prodId);
        }
    }
}